package ru.silviasaint.fridge.server.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ru.silviasaint.fridge.server.bean.Product;
import ru.silviasaint.fridge.server.dao.ProductDao;
import ru.silviasaint.fridge.server.dao.system.AbstractDao;

import com.j256.ormlite.stmt.PreparedQuery;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;

public class ProductDaoImpl extends AbstractDao<Product> implements ProductDao{

	private Logger logger = LoggerFactory.getLogger(ProductDaoImpl.class);
	public ProductDaoImpl() throws SQLException {
		super(Product.class);
	}

	@Override
	public List<Product> getProducts(List<Product> exclude, String name) throws DaoException {
		logger.debug("find products for autocomplete name: " + name);
		QueryBuilder<Product, Integer> builder = this.queryBuilder();
		try {
			Where<Product, Integer> where = builder.where().like("originalText", name);
			if (exclude != null && exclude.size() > 0){
				where = where.and().notIn("id", exclude);
			}
			builder.limit(10L);
			PreparedQuery<Product> query = where.prepare();

			return this.query(query);
		} catch (SQLException e) {
			throw new DaoException(e);
		}
	}

}
